package news

import org.grails.web.json.JSONObject

class newsListController {

    static responseFormats = ['json']

    def index() {
        def re = []
        News.findAll().each {
            re.add([id:it.id,
                    title:it.title,
                    URL:it.URL,
                    createDate:it.createDate,
                    ProduectTypeID: it.productType.id,
                    ProductType: it.productType.productTypeName
            ])
        }
        respond re
    }

    def getNews(){
        def maxrows = Integer.valueOf(params.maxrow?params.maxrow:"5")
        Integer npt = Integer.valueOf(params.pt?params.pt:"5")
        ProductType pt1 = ProductType.get(npt)
        ProductType pt2 = ProductType.get(5)
        VistLogs vistLogs = new VistLogs(visterAddr:request.remoteAddr,
                createDate: new Date(),
                productType: pt1)
        vistLogs.save(flash:true)
        def newsLists = News.findAllByProductTypeOrProductType(pt1, pt2, [max:maxrows,sort:"createDate", order:"desc"])
        def re = []
        newsLists.each {
            re.add([id:it.id,
                    title:it.title,
                    URL: "http://www.68803990.com:8080/News/newsList/vistNews?id=" + it.id.toString() + "&ptid=" + pt1.id.toString(),
                    createDate:it.createDate,
                    ProduectTypeID: it.productType.id,
                    ProductType: it.productType.productTypeName
            ])
        }
        respond re
    }

    def testRespond(){
        def re = []
        re.add([success: true])
        respond re
    }
    def vistNews(){
        Integer id = Integer.parseInt(params.id)
        Integer ptId = Integer.parseInt(params.ptid)

        News m_news = News.get(id)
        ProductType pt = ProductType.get(ptId)
        HitLogs hitLogs = new HitLogs(
                createDate: new Date(),
                visterAddr: request.remoteAddr,
                news: m_news,
                productType: pt
        )
        hitLogs.save(flush:true)
        redirect(url:m_news.URL)
    }
    def testURL(){
        String URL
        //URL = String.format("%s://%s:%s%s")

        URL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + request.getServletPath()
        render(text:URL)
    }
}